﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TheDrinksApp.Models;
using System.Windows.Media;
using TheDrinksApp.Database;

namespace TheDrinksApp.Screen.CurrentOrder.TableViewCell
{
    public partial class AutoSizeTextView : UserControl
    {
        private double LAYOUT_WIDTH;
        public CurrentOrderDAO order;
        public AutoSizeTextView()
        {
            InitializeComponent();
        }

        private void mainGrid_Loaded(object sender, RoutedEventArgs e)
        {
            LAYOUT_WIDTH = 370;
        //    this.Content = null;
            DrawString();
        }
        public string CreateSubString(CurrentOrderDAO order){
            if (order.category.Catergory.ToUpper().Equals(GlobalVariable.BEERS_AND_ALES)
                || order.category.Catergory.ToUpper().Equals(GlobalVariable.WINES)
                || order.category.Catergory.ToUpper().Equals(GlobalVariable.COCKTAILS))
            {
                return "";
            }
            else
            {
                string result = (order.ice ? "ICE ," : "NO ICE ,") + (order.lemon ? " LEMON " : " NO LEMON");
                if (order.category.Catergory.ToUpper().Equals(GlobalVariable.SPIRITS))
                {
                    result += " / " + order.mixer.MixerName;
                }
                return result;
            }
        }
        public void DrawString()
        {
            string s1 = order.drink.DrinkName.ToUpper() ;
            string s2 = " / " + order.size.SizeName.ToUpper();
            string s3 = this.CreateSubString(order);
            double w1, w2;
            w1 = this.measureString(s1, 32);
            w2 = this.measureString(s2, 32);
            Canvas myCanvas = new Canvas();

            if (w1 + w2 >= LAYOUT_WIDTH && s3.Length == 0)
            {
                string[] list = s1.Split(' ');
                TextBlock textBlock = new TextBlock();
                textBlock.Foreground = new SolidColorBrush(Colors.Black);
                textBlock.FontSize = 32;
                textBlock.FontFamily = new System.Windows.Media.FontFamily("/Fonts/Helvetica_Neue_LT_Com.ttf#HelveticaNeueLT Com 77 BdCn");
                for (int i = 0; i < list.Length - 1; i++)
                {
                    textBlock.Text += list[i].ToUpper() + " ";
                }
                Canvas.SetTop(textBlock, 15);
                Canvas.SetLeft(textBlock, 10);
                myCanvas.Children.Add(textBlock);
                TextBlock textBlock2 = new TextBlock();
                textBlock2.Foreground = new SolidColorBrush(Colors.Black);
                textBlock2.FontSize = 32;
                textBlock2.FontFamily = new System.Windows.Media.FontFamily("/Fonts/Helvetica_Neue_LT_Com.ttf#HelveticaNeueLT Com 77 BdCn");
                textBlock2.Text = list[list.Length - 1].ToUpper();
                Canvas.SetTop(textBlock2, 47);
                Canvas.SetLeft(textBlock2, 10);
                myCanvas.Children.Add(textBlock2);
                TextBlock textBlock3 = new TextBlock();
                textBlock3.Foreground = new SolidColorBrush(Colors.Black);
                textBlock3.FontSize = 25;
                textBlock3.FontFamily = new System.Windows.Media.FontFamily("/Fonts/Helvetica_Neue_LT_Com.ttf#HelveticaNeueLT Com 77 BdCn");
                textBlock3.Text =  s2.ToUpper();
                Canvas.SetTop(textBlock3, 54);
                Canvas.SetLeft(textBlock3, this.measureString(textBlock2.Text, 32));
                myCanvas.Children.Add(textBlock3);
        
            }
            else if (w1 + w2 < LAYOUT_WIDTH && s3.Length == 0)
            {
                TextBlock textBlock = new TextBlock();
                textBlock.Foreground = new SolidColorBrush(Colors.Black);
                textBlock.FontSize = 32;
                textBlock.FontFamily = new System.Windows.Media.FontFamily("/Fonts/Helvetica_Neue_LT_Com.ttf#HelveticaNeueLT Com 77 BdCn");
                textBlock.Text = s1.ToUpper();
                Canvas.SetTop(textBlock, 30);
                Canvas.SetLeft(textBlock, 10);
                myCanvas.Children.Add(textBlock);
                TextBlock textBlock3 = new TextBlock();
                textBlock3.Foreground = new SolidColorBrush(Colors.Black);
                textBlock3.FontSize = 25;
                textBlock3.FontFamily = new System.Windows.Media.FontFamily("/Fonts/Helvetica_Neue_LT_Com.ttf#HelveticaNeueLT Com 77 BdCn");
                textBlock3.Text = s2.ToUpper();
                Canvas.SetTop(textBlock3, 37);
                Canvas.SetLeft(textBlock3, this.measureString(textBlock.Text, 32));
                myCanvas.Children.Add(textBlock3);
    
            }
            else
            {
                TextBlock textBlock = new TextBlock();
                textBlock.Foreground = new SolidColorBrush(Colors.Black);
                textBlock.FontSize = 32;
                textBlock.FontFamily = new System.Windows.Media.FontFamily("/Fonts/Helvetica_Neue_LT_Com.ttf#HelveticaNeueLT Com 77 BdCn");
                textBlock.Text = s1.ToUpper();
                Canvas.SetTop(textBlock, 15);
                Canvas.SetLeft(textBlock, 10);
                myCanvas.Children.Add(textBlock);
                TextBlock textBlock3 = new TextBlock();
                textBlock3.Foreground = new SolidColorBrush(Colors.Black);
                textBlock3.FontSize = 25;
                textBlock3.FontFamily = new System.Windows.Media.FontFamily("/Fonts/Helvetica_Neue_LT_Com.ttf#HelveticaNeueLT Com 77 BdCn");
                textBlock3.Text =  s2.ToUpper();
                Canvas.SetTop(textBlock3, 22);
                Canvas.SetLeft(textBlock3, this.measureString(textBlock.Text, 32));
                myCanvas.Children.Add(textBlock3);

                TextBlock textBlock2 = new TextBlock();
                textBlock2.Foreground = new SolidColorBrush(Colors.Black);
                textBlock2.FontSize = 30;
                textBlock2.FontFamily = new System.Windows.Media.FontFamily("/Fonts/Helvetica_Neue_LT_Com.ttf#HelveticaNeueLT Com 77 BdCn");
                textBlock2.Text = s3.ToUpper();
                Canvas.SetTop(textBlock2, 54);
                Canvas.SetLeft(textBlock2, 10);
                myCanvas.Children.Add(textBlock2);
            }
            double long1 = this.measureString(s1, 32) + this.measureString(s2, 25);
            double long2 = this.measureString(s3, 30);
            double mx = Math.Max(long1, long2);
            if (mx > LAYOUT_WIDTH)
            {
                double ratio = LAYOUT_WIDTH / mx;
                ScaleTransform tranform = new ScaleTransform();
                tranform.ScaleX = tranform.ScaleY =(1- ratio /5);
                myCanvas.RenderTransform = tranform;
            }
            this.Content = myCanvas;
        }
        public double measureString(string text, int fontSize)
        {
            double result = 0;
            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == '(' || text[i] == ')' || text[i] == ' ')
                    result += 1.5;
                else
                    result += (fontSize / 2 + 3);
            }
            return result;
        }
    }
}
